package com.heima.search.service.impl;

import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.enums.AppHttpCodeEnum;
import com.heima.model.search.dtos.HistorySearchDto;
import com.heima.search.pojos.ApUserSearch;
import com.heima.model.user.pojos.ApUser;
import com.heima.search.service.ApUserSearchService;
import com.heima.utils.common.ApUserThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

@Service
public class ApUserSearchServiceImpl implements ApUserSearchService {

    @Autowired
    MongoTemplate mongoTemplate;

    @Override
    public void insert(String keyword, Integer userId) {
        //1.根据userid查询用户历史记录
        Query query = Query.query(Criteria.where("userId").is(userId).and(keyword).is(keyword));
        ApUserSearch userSearch = mongoTemplate.findOne(query, ApUserSearch.class);

        if(keyword==null||keyword==""){
            return;
        }
        if(userSearch!=null){
            userSearch.setCreatedTime(new Date());
            mongoTemplate.save(userSearch);
            return;
        }
        userSearch= new ApUserSearch();
        userSearch.setUserId(userId);
        userSearch.setKeyword(keyword);
        //2.判断用户历史记录是否大于十条
        Query query2 = Query.query(Criteria.where("userId").is(userId)).with(Sort.by(Sort.Direction.DESC, "createdTime"));
        List<ApUserSearch> apUserSearches = mongoTemplate.find(query2, ApUserSearch.class);
        if(apUserSearches.size()>=10){
            //如果大于十条则替换最后一条
            ApUserSearch lastApUser = apUserSearches.get(apUserSearches.size()-1);
            ApUserSearch id = mongoTemplate.findAndReplace(Query.query(Criteria.where("id").is(lastApUser.getId())), userSearch);

        }else {
            //如果小于十则直接添加
            mongoTemplate.save(userSearch);
        }

    }



    @Override
    public ResponseResult findUserSearch() {
        ApUser user = ApUserThreadLocalUtil.getThreadLocal();
        if(user.getId()==0){
            return ResponseResult.okResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        Query query = Query.query(Criteria.where("userId").is(user.getId()))
                .with(Sort.by(Sort.Direction.DESC,"createdTime"));

        List<ApUserSearch> apUserSearches = mongoTemplate.find(query, ApUserSearch.class);

        return ResponseResult.okResult(apUserSearches);
    }

    @Override
    public ResponseResult del(HistorySearchDto historySearchDto) {
        if(StringUtils.isEmpty(historySearchDto.getId())){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "id不存在");
        }
        ApUser user = ApUserThreadLocalUtil.getThreadLocal();
        if(user.getId()==0){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "用户未登录");
        }
        mongoTemplate.remove(Query.query(Criteria.where("userId").is(user.getId()).and("id").is(historySearchDto.getId())),ApUserSearch.class);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
